#!/bin/bash

Global_A_Col=""
Global_B_Col=""
Global_C_Col=""
Global_D_Col=""
Global_E_Col=""
Global_F_Col=""
Global_G_Col=""

Global_LightConfig="lightdata.cfg"
Global_LightConfig_Tmp="lightdata.cfg.tmp"
rm -f ${Global_LightConfig_Tmp} ${Global_LightConfig}

#2byte to Big endian
endian_swap()
{
	# Check parameter number
	if [ 1 -ne $# ]; then
		return 1
	fi

	# Check whether parameter is hex string with 8 char number
	if (echo $1 | grep '^[0-9A-Fa-f]\{4\}$') >/dev/null 2>&1; then
		echo -n `echo -n $1 | cut -c 3-4``echo -n $1 | cut -c 1-2`
	elif (echo $1 | grep '^[0-9A-Fa-f]\{8\}$') >/dev/null 2>&1; then
		#output directly
		echo -n $1
	else
		return 2
	fi
}

ProcessExcel()
{
	local Offset Crc_Area

	while read line;
	do
		# Blank started lines ignored
		if (echo "$line" | grep '^[[:space:]]*$\|^[[:space:]]*;') >/dev/null 2>&1; then
			continue
		fi

		Global_D_Col=$(echo "$line" | awk '{print $4}')
		#mkrawinfo cfg need Big endian for two byte
		Global_E_Col="echo $(endian_swap $(echo "${line}" | awk '{print $5}'))"
		Global_F_Col=$(echo "$line" | awk '{print $6}')
		Global_G_Col=$(echo "$line" | awk '{print $7}')

		if [ "x${Global_D_Col}" = "xcrcc" ]; then
			Global_E_Col=""
			Offset="${Global_G_Col}B"
			Crc_Area="24B"
			continue
		fi

		#P -> New column A
		if (echo "$line" | grep '^P') >/dev/null 2>&1; then
			Global_A_Col=$(echo "$line" | awk '{print $1}')
			Global_B_Col=$(echo "$line" | awk '{print $2}')
			Global_C_Col=$(echo "$line" | awk '{print $3}')
		#None None
		elif (echo "$line" | grep -E '^(\bNone\b ){2}[^N]') >/dev/null 2>&1; then
            Global_C_Col=$(echo "$line" | awk '{print $3}')
		#None
		elif (echo "$line" | grep -E '^(\bNone\b ){1}[^N]') >/dev/null 2>&1; then
			Global_B_Col=$(echo "$line" | awk '{print $2}')
			Global_C_Col=$(echo "$line" | awk '{print $3}')
		elif (echo "$line" | grep 'reserved') >/dev/null 2>&1; then
			echo "reserved  #  #  #  #  $(echo "${line}" | awk -F: '{print $2}')B" >> ${Global_LightConfig_Tmp}
			continue
		fi

		if [ "x${Global_D_Col}" = "xNone" ]; then
			echo "${Global_A_Col}-${Global_B_Col}-${Global_C_Col}-${Global_D_Col}  #  #  #  #  ${Global_F_Col}B" >> ${Global_LightConfig_Tmp}
			continue
		fi

		#write to lightdata.cfg
		echo "${Global_A_Col}-${Global_B_Col}-${Global_C_Col}-${Global_D_Col}  #  #  # ${Global_E_Col} #" >> ${Global_LightConfig_Tmp}

		#if STEPID_P5 cal crc
		if [ "x${Global_D_Col}" = "xSTEPID_P5" ]; then
			echo "CRCC_VAL  #  ${Offset}  #  ${Crc_Area}  #  #" >> ${Global_LightConfig_Tmp}
        fi
	done < "$1"
}

CreateHeader()
{
	echo "[global]" > ${Global_LightConfig}
	echo "BLOCK_SIZE=4096" >> ${Global_LightConfig}
	echo -e "FILE=\"lightdata\"\n" >> ${Global_LightConfig}
	echo "[info]" >> ${Global_LightConfig}
}

AlignLeft()
{
	while read line
	do
		line_name=$(echo ${line} | awk -F\# '{print $1}')
		line_crcoffset=$(echo ${line} | awk -F\# '{print $2}')
		line_crcarea=$(echo ${line} | awk -F\# '{print $3}')
		line_inputval=$(echo ${line} | awk -F\# '{print $4}')
		line_reserved=$(echo ${line} | awk -F\# '{print $5}')

		if [ -z "${line_reserved}" ]; then
			printf "%-65s#%-8s#%-8s#%-20s#\n" "${line_name}" "${line_crcoffset}" "${line_crcarea}" "${line_inputval}" >> ${Global_LightConfig}
		else
			printf "%-65s#%-8s#%-8s#%-20s#%8s\n" "${line_name}" "${line_crcoffset}" "${line_crcarea}" "${line_inputval}" "${line_reserved}" >> ${Global_LightConfig}
		fi
	done < ${Global_LightConfig_Tmp}
}

main()
{
	CreateHeader
	sed -i 's/0x//g' $1
	ProcessExcel $1
	AlignLeft
}

main $@
